<template>
	<div>
		<!--高级查询-->
		<el-form :inline="true" :model="query" class="demo-form-inline">
			<el-form-item label="名字">
				<el-input v-model="query.keyword" placeholder="根据名字关键字搜索"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="search">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="edit(null)">新增</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="batchDelete">删除</el-button>
			</el-form-item>
		</el-form>
		<el-table
				:data="pageInfo.rows"
				border
				stripe
				style="width: 94%"
				@selection-change="handleSelectionChange">
			<el-table-column
					type="selection"
					width="55">
			</el-table-column>
			<el-table-column
					fixed
					prop="id"
					label="ID"
					width="50"
					style="display: none">
			</el-table-column>
			<el-table-column
					prop="name"
					label="姓名"
					width="120">
			</el-table-column>
			<el-table-column
					prop="intro"
					label="简介"
					width="120">
			</el-table-column>
			<el-table-column
					prop="parent.name"
					label="上级部门"
					width="200">
			</el-table-column>
			<el-table-column
					prop="manager.id"
					label="部门经理"
					width="120">
			</el-table-column>
			<el-table-column
					prop="create_time"
					label="创建时间"
					width="120">
			</el-table-column>
			<el-table-column
					prop="update_time"
					label="更新时间"
					width="120">
			</el-table-column>
			<el-table-column
					prop="create_user.id"
					label="创建人"
					width="120">
			</el-table-column>
			<el-table-column
					prop="update_user.id"
					label="修改人"
					width="120">
			</el-table-column>
			<el-table-column
					fixed="right"
					label="操作"
					width="100">
				<template slot-scope="scope">
					<el-button @click="deleteById(scope.row)" type="text" size="small">删除</el-button>
					<el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="block">
			<el-pagination
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page.sync="query.currentPage"
					:page-sizes="[10, 20, 30, 40]"
					:page-size="query.pageSize"
					layout="sizes, prev, pager, next"
					:total="pageInfo.total">
			</el-pagination>
		</div>
		<!-- Form -->
		<!--
		<el-dialog title="新增/修改" v-loading="loading" :visible.sync="dialogFormVisible">
			<el-form :model="department">
				<el-form-item label="名字" label-width="120px">
					<el-input v-model="department.name" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="简介" label-width="120px">
					<el-input v-model="department.intro" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="上级部门" label-width="120px">
					<el-input v-model="department.parent.id" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="创建时间" label-width="120px">
					<el-date-picker type="date" placeholder="选择日期" v-model="department.create_time" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="修改时间" label-width="120px">
					<el-date-picker type="date" placeholder="选择日期" v-model="department.update_time" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item label="创建人" label-width="120px">
					<el-input v-model="department.create_user.id" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="修改人" label-width="120px">
					<el-input v-model="department.create_time.id" autocomplete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</div>
		</el-dialog>
		-->
	</div>

</template>

<script>
	export default {
		name: "DepartmentPage",
		data() {
			return {
				query: {
					currentPage: 1,
					pageSize: 5,
					keyword: ""
				},
				pageInfo: {
					rows: null,
					total: null
				},
				dialogFormVisible: false,
				department: {
					id: null,
					name: "",
					intro: "",
					parent: {
						id: null,
						name: ""
					},
					manager: {
						id: null
					},
					create_time: null,
					update_time: null,
					create_user: {
						id: null
					},
					update_user: {
						id: null
					}
				},
				loading: false,
				selections: []
			}
		},
		methods: {
			search() {
				this.getDepartments();
			},
			batchDelete(){
				if(this.selections.length<=0){
					this.getMessage(1,"请先选择要删除的数据！！！")
				}else {
					this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.$http.patch("/batchDelete", this.selections).then(result => {
							if (result.data.success) {
								this.getMessage(0, result.data.message);
								this.getDepartments();
							} else {
								this.getMessage(2, result.data.message);
							}
						}).catch(error => {
							this.getMessage(1, error.message)
						})
					}).catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					})
				}
			},
			edit(val) {
				this.dialogFormVisible = true;
				if (val == null) {
					this.department = {
						id: null,
						name: "",
						intro: "",
						parent: {
							id: null,
							name: ""
						},
						manager: {
							id: null
						},
						create_time: null,
						update_time: null,
						create_user: {
							id: null
						},
						update_user: {
							id: null
						}
					}
				} else {
					this.department = val;
				}

			},
			save() {
				this.$http.put("/department", this.department)
						.then(result => {
							this.loading = true;
							if (result.data.success) {
								this.getMessage(0, result.data.message);
								this.dialogFormVisible = false;
								this.getDepartments();
								this.loading = false
							} else {
								this.getMessage(2, result.data.message);
								this.loading = false
							}
						})
						.catch(error => {
							this.getMessage(1, error.message);
							this.loading = false
						})
			},
			deleteById(val) {
				let user = {
					data: {
						id: val.id
					}
				}
				this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http.delete("/delete", user).then(result => {
						if (result.data.success) {
							this.getMessage(0, result.data.message);
							this.getDepartments();
						} else {
							this.getMessage(2, result.data.message);
						}
					}).catch(error => {
						this.getMessage(1, error.message);
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				})
			},
			getMessage(type, message) {
				if (type == 0) {
					this.$message({
						message: message,
						type: 'success'
					});
				} else if (type == 1) {
					this.$message.error(message);
				} else if (type == 2) {
					this.$message(message)
				}
			},
			handleSizeChange(val) {
				this.query.pageSize = val;
				this.query.currentPage = 1;
				this.getDepartments();
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getDepartments();
			},
			handleSelectionChange(val) {

				console.log(val)
				let ids = val.map(map => map.id)
				this.selections = ids;
			},
			handleClick(val) {
				console.log(val)
			},
			getDepartments() {
				this.$http.post('http://localhost:8080/department', this.query).then(result => {
					result = result.data
					this.pageInfo= result.result;
					console.log(result)
					console.log(this.pageInfo)
				})
			}
		},
		mounted() {
			this.getDepartments();
		}
	}

</script>

<style scoped>

</style>